package com.myself.ssoserver.config

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.scheduling.annotation.AsyncConfigurer
import org.springframework.scheduling.annotation.EnableAsync
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
import org.thymeleaf.spring6.SpringTemplateEngine
import org.thymeleaf.templatemode.TemplateMode
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver
import java.nio.charset.StandardCharsets
import java.util.concurrent.Executor

@Configuration
open class ThymeleafConfig {
    @Bean
    open fun emailTemplateEngine(): SpringTemplateEngine {
        return SpringTemplateEngine().apply {
            addTemplateResolver(htmlTemplateResolver())
            enableSpringELCompiler = true
        }
    }

    private fun htmlTemplateResolver(): ClassLoaderTemplateResolver {
        return ClassLoaderTemplateResolver().apply {
            prefix = "templates/email/"
            suffix = ".html"
            templateMode = TemplateMode.HTML
            characterEncoding = StandardCharsets.UTF_8.name()
        }
    }
}

@Configuration
@EnableAsync
open class AsyncConfig : AsyncConfigurer {
    override fun getAsyncExecutor(): Executor {
        return ThreadPoolTaskExecutor().apply {
            corePoolSize = 5
            maxPoolSize = 10
            queueCapacity = 100
            threadNamePrefix = "EmailExecutor-"
            initialize()
        }
    }
}